package com.maket.scxx.service.impl;

import java.util.List;

import com.maket.scxx.domain.GcFactoryUnitTable;
import com.maket.scxx.domain.TeamMembers;
import com.maket.scxx.feign.GetCodeFeign;
import com.maket.scxx.service.TeamMembersService;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maket.scxx.mapper.BzTeamScheduleMapper;
import com.maket.scxx.domain.BzTeamSchedule;
import com.maket.scxx.service.IBzTeamScheduleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产班组Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@Service
public class BzTeamScheduleServiceImpl implements IBzTeamScheduleService 
{
    @Autowired
    private BzTeamScheduleMapper bzTeamScheduleMapper;

    /**
     * 查询生产班组
     * 
     * @param bzCode 生产班组主键
     * @return 生产班组
     */
    @Override
    public BzTeamSchedule selectBzTeamScheduleByBzCode(String bzCode)
    {
        return bzTeamScheduleMapper.selectBzTeamScheduleByBzCode(bzCode);
    }

    /**
     * 查询生产班组列表
     * 
     * @param bzTeamSchedule 生产班组
     * @return 生产班组
     */
    @Override
    public List<BzTeamSchedule> selectBzTeamScheduleList(BzTeamSchedule bzTeamSchedule)
    {
        return bzTeamScheduleMapper.selectBzTeamScheduleList(bzTeamSchedule);
    }

    /**
     * 新增生产班组
     * 
     * @param bzTeamSchedule 生产班组
     * @return 结果
     */
    @Autowired
    private GetCodeFeign getCodeFeign;
    @Autowired
    private TeamMembersService teamMembersService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBzTeamSchedule(BzTeamSchedule bzTeamSchedule)
    {
        List<TeamMembers> peoples = bzTeamSchedule.getPeoples();
        String code = getCodeFeign.getUser(9);
        bzTeamSchedule.setBzCode(code);
        if (peoples.size() > 0){
            bzTeamScheduleMapper.insertBzTeamSchedule(bzTeamSchedule);
            int i = 0;
            for (TeamMembers people : peoples) {
                people.setBzCode(code);
                people.setCreateMan(SecurityUtils.getUsername());
                teamMembersService.insertPeople(people);
                i++;
            }
            return i;
        }
        return bzTeamScheduleMapper.insertBzTeamSchedule(bzTeamSchedule);
    }

    /**
     * 修改生产班组
     * 
     * @param bzTeamSchedule 生产班组
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBzTeamSchedule(BzTeamSchedule bzTeamSchedule)
    {
        int i = teamMembersService.deleteByBzCode(bzTeamSchedule.getBzCode());
        List<TeamMembers> peoples = bzTeamSchedule.getPeoples();
        if (peoples.size() > 0){
            for (TeamMembers people : peoples) {
                people.setBzCode(bzTeamSchedule.getBzCode());
                people.setUpdateBy(SecurityUtils.getUsername());
                teamMembersService.insertPeople(people);
            }
        }
        return bzTeamScheduleMapper.updateBzTeamSchedule(bzTeamSchedule);
    }
    /**
     * 批量删除生产班组
     * 
     * @param bzCodes 需要删除的生产班组主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBzTeamScheduleByBzCodes(String[] bzCodes)
    {
        for (String bzCode : bzCodes) {
            teamMembersService.deleteByBzCode(bzCode);
        }
        return bzTeamScheduleMapper.deleteBzTeamScheduleByBzCodes(bzCodes);
    }

    /**
     * 删除生产班组信息
     * 
     * @param bzCode 生产班组主键
     * @return 结果
     */
    @Override
    public int deleteBzTeamScheduleByBzCode(String bzCode)
    {
        teamMembersService.deleteByBzCode(bzCode);
        return bzTeamScheduleMapper.deleteBzTeamScheduleByBzCode(bzCode);
    }

    @Override
    public List<GcFactoryUnitTable> getAllScdy(GcFactoryUnitTable gcFactoryUnitTable) {
        return bzTeamScheduleMapper.getAllScdy(gcFactoryUnitTable);
    }
}
